package com.dstz.demo.rest.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.dstz.base.rest.BaseController;
import com.dstz.demo.core.manager.MatecodeManager;
import com.dstz.demo.core.model.Matecode;
import com.dstz.demo.utils.BaseTreeObj;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 物料编码 ��������<br/>
 * @author  aschs
 * </pre>
 */
@RestController
@RequestMapping("/sys/matecode")
class MatecodeController extends BaseController<Matecode>{
    @Resource
    MatecodeManager matecodeManager;
//	@Resource
//	ClclbManager clclbManager;

    @Override
    protected String getModelDesc() {
        return "物料编码";
    }

    /**
     *
     * @param code 最后一级编码
     * @return
     */
    @PostMapping("queryMateCodeByCodeall")
    public List<Matecode> queryMateCodeByCodeall(String code){
        List<Matecode> listall=new ArrayList<Matecode>();
        List<Matecode>  codelist=queryMateCodeByCode(code);
        listall.addAll(codelist);
        int codestr=code.indexOf("\\.");
        if(codestr>-1){
            String pcode=code.substring(0,code.lastIndexOf("\\."));
            List<Matecode>  pcodelist=queryMateCodeByCode(pcode);
            listall.addAll(pcodelist);
        }
        return listall;
    }

    @PostMapping("/save/{id}")
    public String save(@PathVariable("id") String id){
        String[] num=id.split(",");
        List<Matecode> ckey=matecodeManager.lists(num[0]);
        String name=num[1];
        String[] ma=num[0].split("\\.");
        if(0<ckey.size()){
            Integer b=0;
            Integer c=0;
            Integer d=0;
            String f="";
            for (int i=0;i<ckey.size();i++){
                f=ckey.get(i).getCkey().substring(0,ckey.get(i).getCkey().lastIndexOf(".")+1);
                b=Integer.parseInt(ckey.get(i).getCkey().substring(ckey.get(i).getCkey().lastIndexOf(".")+1));
                if(b>c){
                    c=b;
                    d=c;
                }
            }
            d+=1;
            String adc=d.toString();
            if(adc.length()==1){
                Matecode matecodeDO=new Matecode();
                matecodeDO.setCkey(f+"0"+adc);
                matecodeDO.setCname(name);
                matecodeDO.setPkey(num[0]);
                matecodeDO.setClevel(ma.length);
                matecodeManager.save(matecodeDO);
            }else{
                Matecode matecodeDO=new Matecode();
                matecodeDO.setCkey(f+adc);
                matecodeDO.setCname(name);
                matecodeDO.setPkey(num[0]);
                matecodeDO.setClevel(ma.length);
                matecodeManager.save(matecodeDO);
            }
        }
        if(0==ckey.size()){
//			Clclb clclb=clclbManager.gets(num[3]);
//			clclb.setWlbm(num[0]+".01");
//			clclb.setCpmc(num[1]);
//			clclb.setId(num[3]);
//			clclbManager.updates(clclb);
            Matecode matecodeDO=new Matecode();
            matecodeDO.setCkey(num[0]+".01");
            matecodeDO.setCname(num[1]);
            matecodeDO.setPkey(num[0]);
            matecodeDO.setClevel(ma.length+1);
            matecodeManager.save(matecodeDO);
        }
        return "成功";
    }

    /**
     *
     * @return
     */
    @PostMapping("queryMateCodeByCode")
    public List<Matecode> queryMateCodeByCode(String code){
        Map<String,Object> mapparams=new HashMap<String,Object>();
        mapparams.put("whereSql","where ckey='"+code+"'");
        List<Matecode>  codelist=this.matecodeManager.queryByCode(mapparams);

        return null;
    }

    /**
     * 查询所有的节点
     * @return
     */
    @GetMapping("findAll")
    public String findAll(){
        List<BaseTreeObj> all = matecodeManager.findAll();
        String s = JSON.toJSONString(all, SerializerFeature.DisableCircularReferenceDetect);
//        String s1 = StringEscapeUtils.unescapeJava(s);
//        System.out.println(s1);
        return s;
    }

    @GetMapping("findthree")
    public List<Matecode> findthree(){
        List<Matecode> list=matecodeManager.findthree();
        return list;
    }

    @GetMapping("/findMatecode/{wlbm}")
    public Matecode findMatecode(@PathVariable("wlbm") String wlbm){
       return matecodeManager.findMatecode(wlbm);
    }
}
